<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>numpy.piecewise &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../about.html" >
    <link rel="index" title="Index" href="../../genindex.html" >
    <link rel="search" title="Search" href="../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../index.html" >
    <link rel="up" title="Functional programming" href="../routines.functional.html" >
    <link rel="next" title="NumPy-specific help functions" href="../routines.help.html" >
    <link rel="prev" title="numpy.frompyfunc" href="numpy.frompyfunc.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../index.html">
      <img border=0 alt="NumPy" src="../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../routines.html" >Routines</a></li>
          <li class="active"><a href="../routines.functional.html" accesskey="U">Functional programming</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="../routines.help.html" title="NumPy-specific help functions"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="numpy.frompyfunc.html" title="numpy.frompyfunc"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="numpy.frompyfunc.html"
                        title="previous chapter">numpy.frompyfunc</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="../routines.help.html"
                        title="next chapter">NumPy-specific help functions</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="numpy-piecewise">
<h1>numpy.piecewise<a class="headerlink" href="#numpy-piecewise" title="Permalink to this headline">¶</a></h1>
<dl class="function">
<dt id="numpy.piecewise">
<code class="sig-prename descclassname">numpy.</code><code class="sig-name descname">piecewise</code><span class="sig-paren">(</span><em class="sig-param">x</em>, <em class="sig-param">condlist</em>, <em class="sig-param">funclist</em>, <em class="sig-param">*args</em>, <em class="sig-param">**kw</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/lib/function_base.py#L511-L630"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.piecewise" title="Permalink to this definition">¶</a></dt>
<dd><p>Evaluate a piecewise-defined function.</p>
<p>Given a set of conditions and corresponding functions, evaluate each
function on the input data wherever its condition is true.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl>
<dt><strong>x</strong><span class="classifier">ndarray or scalar</span></dt><dd><p>The input domain.</p>
</dd>
<dt><strong>condlist</strong><span class="classifier">list of bool arrays or bool scalars</span></dt><dd><p>Each boolean array corresponds to a function in <em class="xref py py-obj">funclist</em>.  Wherever
<em class="xref py py-obj">condlist[i]</em> is True, <em class="xref py py-obj">funclist[i](x)</em> is used as the output value.</p>
<p>Each boolean array in <em class="xref py py-obj">condlist</em> selects a piece of <em class="xref py py-obj">x</em>,
and should therefore be of the same shape as <em class="xref py py-obj">x</em>.</p>
<p>The length of <em class="xref py py-obj">condlist</em> must correspond to that of <em class="xref py py-obj">funclist</em>.
If one extra function is given, i.e. if
<code class="docutils literal notranslate"><span class="pre">len(funclist)</span> <span class="pre">==</span> <span class="pre">len(condlist)</span> <span class="pre">+</span> <span class="pre">1</span></code>, then that extra function
is the default value, used wherever all conditions are false.</p>
</dd>
<dt><strong>funclist</strong><span class="classifier">list of callables, f(x,*args,**kw), or scalars</span></dt><dd><p>Each function is evaluated over <em class="xref py py-obj">x</em> wherever its corresponding
condition is True.  It should take a 1d array as input and give an 1d
array or a scalar value as output.  If, instead of a callable,
a scalar is provided then a constant function (<code class="docutils literal notranslate"><span class="pre">lambda</span> <span class="pre">x:</span> <span class="pre">scalar</span></code>) is
assumed.</p>
</dd>
<dt><strong>args</strong><span class="classifier">tuple, optional</span></dt><dd><p>Any further arguments given to <a class="reference internal" href="#numpy.piecewise" title="numpy.piecewise"><code class="xref py py-obj docutils literal notranslate"><span class="pre">piecewise</span></code></a> are passed to the functions
upon execution, i.e., if called <code class="docutils literal notranslate"><span class="pre">piecewise(...,</span> <span class="pre">...,</span> <span class="pre">1,</span> <span class="pre">'a')</span></code>, then
each function is called as <code class="docutils literal notranslate"><span class="pre">f(x,</span> <span class="pre">1,</span> <span class="pre">'a')</span></code>.</p>
</dd>
<dt><strong>kw</strong><span class="classifier">dict, optional</span></dt><dd><p>Keyword arguments used in calling <a class="reference internal" href="#numpy.piecewise" title="numpy.piecewise"><code class="xref py py-obj docutils literal notranslate"><span class="pre">piecewise</span></code></a> are passed to the
functions upon execution, i.e., if called
<code class="docutils literal notranslate"><span class="pre">piecewise(...,</span> <span class="pre">...,</span> <span class="pre">alpha=1)</span></code>, then each function is called as
<code class="docutils literal notranslate"><span class="pre">f(x,</span> <span class="pre">alpha=1)</span></code>.</p>
</dd>
</dl>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><dl class="simple">
<dt><strong>out</strong><span class="classifier">ndarray</span></dt><dd><p>The output is the same shape and type as x and is found by
calling the functions in <em class="xref py py-obj">funclist</em> on the appropriate portions of <em class="xref py py-obj">x</em>,
as defined by the boolean arrays in <em class="xref py py-obj">condlist</em>.  Portions not covered
by any condition have a default value of 0.</p>
</dd>
</dl>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="numpy.choose.html#numpy.choose" title="numpy.choose"><code class="xref py py-obj docutils literal notranslate"><span class="pre">choose</span></code></a>, <a class="reference internal" href="numpy.select.html#numpy.select" title="numpy.select"><code class="xref py py-obj docutils literal notranslate"><span class="pre">select</span></code></a>, <a class="reference internal" href="numpy.where.html#numpy.where" title="numpy.where"><code class="xref py py-obj docutils literal notranslate"><span class="pre">where</span></code></a></p>
</div>
<p class="rubric">Notes</p>
<p>This is similar to choose or select, except that functions are
evaluated on elements of <em class="xref py py-obj">x</em> that satisfy the corresponding condition from
<em class="xref py py-obj">condlist</em>.</p>
<p>The result is:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>      <span class="o">|--</span>
      <span class="o">|</span><span class="n">funclist</span><span class="p">[</span><span class="mi">0</span><span class="p">](</span><span class="n">x</span><span class="p">[</span><span class="n">condlist</span><span class="p">[</span><span class="mi">0</span><span class="p">]])</span>
<span class="n">out</span> <span class="o">=</span> <span class="o">|</span><span class="n">funclist</span><span class="p">[</span><span class="mi">1</span><span class="p">](</span><span class="n">x</span><span class="p">[</span><span class="n">condlist</span><span class="p">[</span><span class="mi">1</span><span class="p">]])</span>
      <span class="o">|...</span>
      <span class="o">|</span><span class="n">funclist</span><span class="p">[</span><span class="n">n2</span><span class="p">](</span><span class="n">x</span><span class="p">[</span><span class="n">condlist</span><span class="p">[</span><span class="n">n2</span><span class="p">]])</span>
      <span class="o">|--</span>
</pre></div>
</div>
<p class="rubric">Examples</p>
<p>Define the sigma function, which is -1 for <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">0</span></code> and +1 for <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&gt;=</span> <span class="pre">0</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mf">2.5</span><span class="p">,</span> <span class="mf">2.5</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">piecewise</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">,</span> <span class="n">x</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>
<span class="go">array([-1., -1., -1.,  1.,  1.,  1.])</span>
</pre></div>
</div>
<p>Define the absolute value, which is <code class="docutils literal notranslate"><span class="pre">-x</span></code> for <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&lt;0</span></code> and <code class="docutils literal notranslate"><span class="pre">x</span></code> for
<code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&gt;=</span> <span class="pre">0</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">piecewise</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">,</span> <span class="n">x</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="o">-</span><span class="n">x</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">])</span>
<span class="go">array([2.5,  1.5,  0.5,  0.5,  1.5,  2.5])</span>
</pre></div>
</div>
<p>Apply the same function to a scalar value.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="o">-</span><span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">piecewise</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="p">[</span><span class="n">y</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">,</span> <span class="n">y</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="o">-</span><span class="n">x</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">])</span>
<span class="go">array(2)</span>
</pre></div>
</div>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>